package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.ReentrantLock;

public class Demo6 {
    private static Integer count = 0;

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ReentrantLock lock = new ReentrantLock(true);


        Thread t = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                try {
                    if(lock.tryLock()) {
                        count++;
                    } else {
                        //没获取到锁 可以去执行其他逻辑 可以等待 也可以直接返回
                    }
                } finally {
                    lock.unlock();
                }

            }
        });
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                try {
                    lock.lock();
                    count++;
                } finally {
                    lock.unlock();
                }
            }
        });
        t.start();
        t1.start();
        t1.join();
        t.join();
        System.out.println(count);
    }
}


